package com.clq.utils;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;

/**
 * 响应工具
 * @author chenluqiang
 * @date 2019年3月30日 上午10:02:28 
 *
 */
public class ResponseUtils {
	
	/**
	 * 响应页面还是json
	 * @author chenluqiang
	 * @date 2019年3月30日 上午10:02:40 
	 * @param result
	 * @param flag
	 */
	public static void returnJsonOrPage(HttpServletRequest req,HttpServletResponse res,Object result,boolean flag) {
		PrintWriter writer=null;
		try {
			res.setCharacterEncoding("utf-8");
			writer = res.getWriter();
			if(result!=null&&result.toString()!=""&&result.toString().length()>0) {
				if(flag) {
					res.sendRedirect(req.getServletContext().getContextPath()+"/"+result.toString()+".html");
				}else {
					res.setContentType("application/json");
					Map<String,Object> resultMap = new HashMap<String,Object>();
					resultMap.put(Constant.STATUS, Constant.SUCCESS);
					resultMap.put(Constant.RESULT,result);
					writer.println(JSON.toJSONString(resultMap));
				}
			}else {
				writer.println("NOT RESULT!");
			}
		} catch (IOException e) {
			writer.println("Exception:"+e.getMessage());
			e.printStackTrace();
		}
	}
}
